/**
 * COPYRIGHT (C) 2009. Denny Tran, Omar Jaime, David Deng, Julie Hsieh. All Rights Reserved
 * A clickable component on the calendar that represents an event.  
 * @author Denny Tran, Omar Jaime, David Deng, Julie Hsieh 
 * @version 1.01 2009-05-05
 */

package gui;

import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.BorderFactory;
import javax.swing.JTextArea;
import data.Event;

public class EventComponent extends JTextArea
{
    public MouseAdapter listener;
    private Event currentEvent;
    
    /**
     * Constructs an event component relative to the given size.
     * @param viewManager The view manager to attach to to this component.
     * @param e The event to set this components size and location to.
     */
    public EventComponent(final ViewManager viewManager, Event e)
    {
        currentEvent = e;
        this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        this.setFont(new Font("Serif", Font.PLAIN, 10));
        this.setBorder(BorderFactory.createRaisedBevelBorder());
        
        if (currentEvent.getType().equals("deadline"))
            this.setText(currentEvent.getStartTimeString() + '\n' + currentEvent.getEventName());
        else
            this.setText(currentEvent.getStartTimeString()+"-"+currentEvent.getEndTimeString()+'\n'+
                currentEvent.getEventName());
        
        // set colors
        if (currentEvent.getType().equals("event"))
            this.setBackground(new Color(150,255,0));
        else if (currentEvent.getType().equals("meeting"))
            this.setBackground(Color.red);
        else if (currentEvent.getType().equals("deadline"))
            this.setBackground(Color.CYAN);
        
        double start =  currentEvent.getStartTime().get(Calendar.HOUR_OF_DAY) + currentEvent.getStartTime().get(Calendar.MINUTE)/60.0;
        double end = currentEvent.getEndTime().get(Calendar.HOUR_OF_DAY) + currentEvent.getEndTime().get(Calendar.MINUTE)/60.0;
        double duration = end - start;
        
        if (currentEvent.getType().equals("deadline"))
        {
            this.setSize(CalendarComponent.DAY_WIDTH, (int)(CalendarComponent.HOUR_SIZE/2 + 5));
            this.setLocation(CalendarComponent.STARTX + 1 + CalendarComponent.DAY_WIDTH * (currentEvent.getStartTime().get(Calendar.DAY_OF_WEEK) - 1),
                  30 + (int)(start * CalendarComponent.HOUR_SIZE)-this.getHeight());
        }
        else
        {
            this.setSize(CalendarComponent.DAY_WIDTH, (int)(CalendarComponent.HOUR_SIZE*duration));
        
            this.setLocation(CalendarComponent.STARTX + 1 + CalendarComponent.DAY_WIDTH * (currentEvent.getStartTime().get(Calendar.DAY_OF_WEEK) - 1),         
                    30 + (int)(start * CalendarComponent.HOUR_SIZE));
        }
        this.setLineWrap(true);
        this.setEditable(false);
        
        listener = new MouseAdapter()
        {
            public void mousePressed(MouseEvent me)
            {
               setBorder(BorderFactory.createLoweredBevelBorder());
            }
            
            public void mouseReleased(MouseEvent me)
            {
               setBorder(BorderFactory.createRaisedBevelBorder());
               if (contains(me.getPoint()))
               {
                   String currentUser = viewManager.getGs().getUser().getUserName();
                   if (currentEvent.getUserName().isEmpty() || currentUser.equals(currentEvent.getUserName()))
                   {
                       if (currentEvent.getType().equals("event"))
                       {
                           viewManager.showView("add event");
                           AddEvent view = (AddEvent) viewManager.getView("add event");
                           view.setView(currentEvent);
                           
                       }
                       else if (currentEvent.getType().equals("meeting"))
                       {
                           if (viewManager.getGs().getUser().isAdmin())
                           {
                               viewManager.showView("add meeting");
                               AddMeeting view = (AddMeeting)viewManager.getView("add meeting");
                               view.setView(currentEvent);
                           }
                           else
                           {
                               viewManager.showView("show event");
                               ShowEvent view = (ShowEvent) viewManager.getView("show event");
                               view.setEvent(currentEvent);
                           }
                       }
                       else if (currentEvent.getType().equals("deadline"))
                       {
                           viewManager.showView("add deadline");
                           AddDeadline view = (AddDeadline)viewManager.getView("add deadline");
                           view.setView(currentEvent);
                       }
                   }
                   else
                   {
                       viewManager.showView("show event");
                       ShowEvent view = (ShowEvent) viewManager.getView("show event");
                       view.setEvent(currentEvent);
                   }
               }
            }
        };
        
        this.addMouseListener(listener);
    }
    
    /**
     * Gets the current event of this component.
     * @return The current event of this component.
     */
    public Event getCurrentEvent()
    {
        return currentEvent;
    }
    
    /**
     * Sets the current event of this component.
     * @param currentEvent
     */
    public void setCurrentEvent(Event currentEvent)
    {
        this.currentEvent = currentEvent;
    }
}
